package yang.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import yang.dao.BaseDaoI;
import yang.model.HospitalOffice;
import yang.model.HospitalUser;
import yang.pageModel.Json;
import yang.pageModel.User;
import yang.service.UserServiceI;
import yang.util.ComBox;
import yang.util.Encrypt;

/**
 * 用户服务的实现
 * 
 * @ClassName: UserServiceImpl
 * @Description: TODO
 * @author: Administrator
 * @date: 2014年6月13日 上午8:47:56
 */
@Service("userService")
public class UserServiceImpl implements UserServiceI {

	private static final Logger logger = Logger
			.getLogger(UserServiceImpl.class);

	private BaseDaoI<HospitalUser> userDao;
	private BaseDaoI<HospitalOffice> officeDao;

	/**
	 * 
	 * @Title: save
	 * @Description: 新建一个用户
	 * @param user
	 * @return
	 * @see yang.service.UserServiceI#save(yang.pageModel.User)
	 */
	@Override
	public Serializable save(User user) {
		// 创建一个用户
		HospitalUser hospitalUser = new HospitalUser();
		// 创建一个科室
		HospitalOffice office = new HospitalOffice();
		// 为用户密码进行加密
		String pwd = Encrypt.e(user.getUserPwd());
		user.setUserPwd(pwd);
		// 给实体赋值
		BeanUtils.copyProperties(user, hospitalUser);
		// 进行验证，如果需要添加科室就为用户添加科室
		if (user.getOfficeId().equals("")) {
			return userDao.save(hospitalUser);
		} else {
			office = officeDao.get(HospitalOffice.class,
					Integer.parseInt(user.getOfficeId()));
			hospitalUser.setHospitalOffice(office);
		}

		logger.info("保存了一个用户");
		return userDao.save(hospitalUser);
	}

	/**
	 * 
	 * @Title: delete
	 * @Description: 删除用户的方法，没有被用到
	 * @param user
	 * @see yang.service.UserServiceI#delete(yang.model.HospitalUser)
	 */
	@Override
	public void delete(HospitalUser user) {
		userDao.delete(user);

	}

	/**
	 * 
	 * @Title: getUserPrice
	 * @Description: 获取医生的挂号费
	 * @param userid
	 * @return
	 * @see yang.service.UserServiceI#getUserPrice(int)
	 */
	@Override
	public Json getUserPrice(int userid) {
		HospitalUser user = userDao.get(HospitalUser.class, userid);
		Json json = new Json();
		json.setObj(user.getUserPrice());
		return json;
	}

	/**
	 * 
	 * @Title: login
	 * @Description: 登录
	 * @param user
	 * @return
	 * @see yang.service.UserServiceI#login(yang.pageModel.User)
	 */
	@Override
	public User login(User user) {
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("userNicheng", user.getUserNicheng());
		m.put("userPwd", Encrypt.e(user.getUserPwd()));
		HospitalUser u = userDao
				.get("from HospitalUser h where h.userNicheng=:userNicheng and h.userPwd=:userPwd",
						m);
		User t = new User();
		BeanUtils.copyProperties(u, t);
		logger.info("按条件找到用户");
		return t;

	}

	/**
	 * 
	 * @Title: findUserByOffice
	 * @Description: 得到科室下面的用户
	 * @param ofid
	 * @return
	 * @see yang.service.UserServiceI#findUserByOffice(int)
	 */
	@Override
	public List<ComBox> findUserByOffice(int ofid) {
		//建立一个下拉框集合
		List<ComBox> boxs = new ArrayList<ComBox>();
		//设置查询条件
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("ofid", ofid);
		//查询集合
		List<HospitalUser> list = userDao
				.find("from HospitalUser h where h.hospitalOffice.ofId=:ofid",
						params);
		for (HospitalUser hospitalUser : list) {
			ComBox box = new ComBox();
			box.setValue(hospitalUser.getUserNo().toString());
			box.setText(hospitalUser.getUserNicheng());
			boxs.add(box);
		}
		logger.info("查到科室");
		return boxs;
	}

	// ----- get set ------
	public BaseDaoI<HospitalUser> getUserDao() {
		return userDao;
	}

	@Autowired
	public void setUserDao(BaseDaoI<HospitalUser> userDao) {
		this.userDao = userDao;
	}

	public BaseDaoI<HospitalOffice> getOfficeDao() {
		return officeDao;
	}

	@Autowired
	public void setOfficeDao(BaseDaoI<HospitalOffice> officeDao) {
		this.officeDao = officeDao;
	}

}
